﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WEBDT.Base;
using WEBDT.ViewModel;

namespace WEBDT.Areas.Admin.Controllers
{
    public class NhapHangController : BaseController<NhapHang>
    {
        //
        // GET: /Admin/NhapHang/

        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public override ActionResult insert()
        {
            var model = new NhapHangModel
            {
                tontai = false,
            };
            ViewData["username"] = new SelectList(repository.Danhsach<Account>(), "username", "username");
            return View(model);
        }

        [HttpPost]
        public override ActionResult insert(FormCollection fr)
        {
            var ht = new NhapHangModel
            {
                Username = fr.Get("username"),
                TongTien = Convert.ToInt32(fr.Get("tongtien")),
                NgayNhap = fr.Get("ngaynhap"),
                Status = Convert.ToInt32(fr.Get("status")),

            };
            var model = new NhapHang
            {
                Username = ht.Username,
                TongTien = ht.TongTien,
                NgayNhap = ht.NgayNhap,
                Status = ht.Status,

            };
            repository.Insert<NhapHang>(model);
            return RedirectToAction("getall");

        }

        [HttpGet]
        public override ActionResult update(int id)
        {
            var ht = repository.SelectByID<NhapHang>(id);
            var model = new NhapHangModel
            {
                MaNH = ht.MaNH,
                Username = ht.Username,
                TongTien = Convert.ToInt32(ht.TongTien),
                NgayNhap = ht.NgayNhap,
                Status = Convert.ToInt32(ht.Status),

            };
            ViewData["username"] = new SelectList(repository.Danhsach<Account>(), "username", "username");
            return View(model);
        }

        [HttpPost]
        public override ActionResult update(FormCollection fr)
        {
            var ht = new NhapHangModel
            {
                MaNH = Convert.ToInt32(fr.Get("maNH")),
                Username = fr.Get("username"),
                TongTien = Convert.ToInt32(fr.Get("tongtien")),
                NgayNhap = fr.Get("ngaynhap"),
                Status = Convert.ToInt32(fr.Get("status")),
            };

            var model = repository.SelectByID<NhapHang>(ht.MaNH);
            model.Username = ht.Username;
            model.NgayNhap = ht.NgayNhap;
            model.TongTien = ht.TongTien;
            model.Status = ht.Status;
            repository.commitchanges();
            return RedirectToAction("getall");
        }
    }
}
